package ar.edu.unicen.exa.isistan.archassis.ia.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ar.edu.unicen.exa.isistan.archassis.ia.Result;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.ItemCase;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.ia.util.AverageCalculator;

public class ItemCaseEvalResult {
	private ListCasesEvalResult listCasesEvalResult;
	private ItemCase itemCase;
	//private Map<JavaClass, JavaClass> mapByCaseJavaClass;
	private Map<JavaClass, Result> mapByInputJavaClass;
	private AverageCalculator hechoAvg;
	private AverageCalculator sobraAvg;
	
	public ItemCaseEvalResult(ListCasesEvalResult listCasesEvalResult, ItemCase itemCase) {
		this.listCasesEvalResult = listCasesEvalResult;
		this.itemCase = itemCase;
		this.hechoAvg = new AverageCalculator();
		this.sobraAvg = new AverageCalculator();
		
		//this.mapByCaseJavaClass = new HashMap<JavaClass, JavaClass>();
		this.mapByInputJavaClass = new HashMap<JavaClass, Result>();
	}

	
	/**
	 * Evalua las dos clases y las agrega a la lista.
	 * @param caseJavaClass
	 * @param inputJavaClass
	 */
	public void add(JavaClass caseJavaClass, JavaClass inputJavaClass) {
		float hecho = caseJavaClass.eval(inputJavaClass);
		float sobra = 1 - inputJavaClass.eval(caseJavaClass);
		
		Result result = new Result(inputJavaClass, caseJavaClass, listCasesEvalResult.getListCasesBean(), hecho, sobra);
		Result oldResult = mapByInputJavaClass.get(caseJavaClass);
		if (result.betterThan(oldResult)) {
			mapByInputJavaClass.put(caseJavaClass, result);
			hechoAvg.add(result.getCumplido());
			sobraAvg.add(result.getSobra());
			if (oldResult != null) {
				hechoAvg.rem(oldResult.getCumplido());
				sobraAvg.rem(oldResult.getSobra());
			}
		}

	}
	
	public float getHecho() {
		return hechoAvg.getAVG();
	}
	public float getSobra() {
		return sobraAvg.getAVG();
	}


	public List<Result> getListResult(ListCasesEvalResult listCasesEvalResult) {
		List<Result> result = new ArrayList<Result>();
		result.addAll(mapByInputJavaClass.values());
		return result;
	}
}
